package com.kepler.social.common.utils;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;


public class DateTimeUtil {

    public static final long SECOND = 1L;
    public static final long MINUTE = 60*SECOND;
    public static final long HOUR   = 60*MINUTE;
    public static final long DAY    = 24*HOUR;

    public static long toMilli(LocalDateTime localDateTime){
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Duration between(LocalDateTime startDt, LocalDateTime endDt){
        return Duration.between(startDt,endDt);
    }

    public static Duration between(Long startDt, Long endDt){
        return between(fromMilli(startDt), fromMilli(endDt));
    }

    public static LocalDateTime fromMilli(Long dt){
        return Instant.ofEpochMilli(dt).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDateTime fromDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * 开始的时间
     * 2024-02-15 00:00:00
     * @param localDate
     * @return
     */
    public static LocalDateTime startTime(LocalDate localDate) {
        return localDate.atTime(LocalTime.MIN);
    }

    /**
     * 结束的时间
     * 2024-02-15 23:59:59
     * @param localDate
     * @return
     */
    public static LocalDateTime endTime(LocalDate localDate) {
        return localDate.atTime(LocalTime.MAX);
    }

}